package com.controller;

import com.mapper.OperateRecordsMapper;
import com.model.OperateRecords;
import com.model.ResponseMsg;
import com.model.User;
import com.util.ConstantUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@PropertySource("classpath:message.properties")
public class RecordsController {


    @Autowired
    private Environment env;


    @Autowired
    private OperateRecordsMapper operateRecordsMapper;


    @Value("${welcome.message:test}")
    private String message = "Hello World";


    @RequestMapping("/records")
    public String operateRecordList(Map<String, Object> model, HttpServletRequest request) {

        User user = (User) request.getSession().getAttribute(ConstantUtil.loginSession);
        String type = request.getParameter("type");
        String timeRange = request.getParameter("timeRange");
        ResponseMsg msg = new ResponseMsg();
        OperateRecords record = new OperateRecords();
        record.setUserId(user.getUserId());
        HttpSession session = request.getSession();
        Object obj = session.getAttribute("recordType");

        if(StringUtils.isBlank(type) && obj == null){
            record.setType("1");
            session.setAttribute("recordType",type);
        }else if(StringUtils.isNotBlank(type)){
            session.setAttribute("recordType",type);
            record.setType(type);
        }else{
            String tempType = (String)obj;
            record.setType(tempType+"");
        }

        Object rangeTimeObj = session.getAttribute("recordtimeRange");
        if(StringUtils.isBlank(timeRange) && rangeTimeObj == null){
            timeRange = "1";
            Date date = DateUtils.addDays(new Date(), -1);
            record.setCreatetime(date);
            session.setAttribute("recordtimeRange",timeRange);
        }else if(StringUtils.isNotBlank(timeRange)){
            session.setAttribute("recordtimeRange",timeRange);
            Date date = DateUtils.addDays(new Date(), -Integer.parseInt(timeRange));
            record.setCreatetime(date);
        }else{
            String tempDate =(String) rangeTimeObj;
            Date date = DateUtils.addDays(new Date(), -Integer.parseInt(tempDate));
            record.setCreatetime(date);
        }

        List<OperateRecords> list = operateRecordsMapper.getOperateRecordByConditon(record);
        model.put("pagename", "records");
        model.put("list", list);
        return "usercenter";
    }

    @RequestMapping("/operateRecordList")
    @ResponseBody
    public ResponseMsg operateRecordList(HttpServletRequest request) {

        User user = (User) request.getSession().getAttribute(ConstantUtil.loginSession);
        String type = request.getParameter("type");
        String timeRange = request.getParameter("timeRange");

        ResponseMsg msg = new ResponseMsg();

        OperateRecords record = new OperateRecords();
        record.setUserId(user.getUserId());
        record.setType(type);
        Date date = DateUtils.addDays(new Date(), Integer.parseInt(timeRange));
        record.setCreatetime(date);

        List<OperateRecords> list = operateRecordsMapper.getOperateRecordByConditon(record);
        msg.setStatus(ResponseMsg.success);
        msg.setData(list);
        return msg;
    }


    public OperateRecordsMapper getOperateRecordsMapper() {
        return operateRecordsMapper;
    }

    public void setOperateRecordsMapper(OperateRecordsMapper operateRecordsMapper) {
        this.operateRecordsMapper = operateRecordsMapper;
    }

    public Environment getEnv() {
        return env;
    }

    public void setEnv(Environment env) {
        this.env = env;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


}
